package com.hxd.otc.module.convenience.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.enums.ConvenienceBusinessTypeEnum;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.PhoneLocationBean;
import com.hxd.otc.module.convenience.bean.PrepaidGoodsBean;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.contract.PrepaidRechargeContract;
import com.hxd.otc.module.convenience.model.ConvenienceModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

/**
 * Created by lzy on 2018/12/13 0013.
 * description ${话费充值 Presenter}
 */
public class PrepaidRechargePresenter extends BasePresenter<PrepaidRechargeContract.IView> implements PrepaidRechargeContract.IPresenter {

    private final ConvenienceModel mConvenienceModel;
    private RefuelingServiceBean mRefuelingServiceBean;
    private ConvenienceBusinessTypeEnum mBusinessType;

    private HashMap<String, PhoneLocationBean> mPhoneLocationCacheHashMap = new HashMap<>();

    public PrepaidRechargePresenter(PrepaidRechargeContract.IView iView) {
        super(iView);
        mConvenienceModel = ConvenienceModel.getInstance();
    }

    @Override
    public void getPrepaidRechargeInfo() {
        mConvenienceModel.getGoodsList(mRefuelingServiceBean.getId(), new BaseObserver<ArrayList<NorGoodsBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<NorGoodsBean> norGoodsBeans) {
                TreeMap<Double, PrepaidGoodsBean> treeMap = new TreeMap<>();
                for (NorGoodsBean norGoodsBean : norGoodsBeans) {
                    double sellPrice = norGoodsBean.getSellPrice();
                    if (treeMap.containsKey(sellPrice)) {
                        treeMap.get(sellPrice).getNorGoodsBeans().add(norGoodsBean);
                    } else {
                        PrepaidGoodsBean prepaidGoodsBean = new PrepaidGoodsBean();
                        prepaidGoodsBean.setSellPrice(sellPrice);
                        prepaidGoodsBean.setVolume(norGoodsBean.getVolume());
                        prepaidGoodsBean.setSymbol(norGoodsBean.getSymbol());
                        prepaidGoodsBean.getNorGoodsBeans().add(norGoodsBean);
                        treeMap.put(sellPrice, prepaidGoodsBean);
                    }
                }
                mView.requestPrepaidRechargeInfoSuccess(new ArrayList<>(treeMap.values()));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<NorGoodsBean>>>bindAutoDispose());
    }

    @Override
    public PhoneLocationBean getPhoneLocation(final String phoneNumber) {
        if (mPhoneLocationCacheHashMap.containsKey(phoneNumber)) {
            return mPhoneLocationCacheHashMap.get(phoneNumber);
        } else {
            requestPhoneLocation(phoneNumber);
            return null;
        }
    }

    @Override
    public void requestPhoneLocation(final String phoneNumber) {
        mConvenienceModel.getPhoneLocation(phoneNumber, new BaseObserver<PhoneLocationBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull PhoneLocationBean phoneLocation) {
                mPhoneLocationCacheHashMap.put(phoneNumber, phoneLocation);//进行缓存
                mView.getPhoneLocationSuccess(phoneNumber, phoneLocation);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.getPhoneLocationFail(phoneNumber, errMessage);
            }
        }, mView.<BaseBean<PhoneLocationBean>>bindAutoDispose());
    }

    @Override
    public void setRefuelingServiceBean(RefuelingServiceBean refuelingServiceBean) {
        mRefuelingServiceBean = refuelingServiceBean;
    }

    @Override
    public void setBusinessType(ConvenienceBusinessTypeEnum businessType) {
        mBusinessType = businessType;
    }

    @Override
    public ConvenienceBusinessTypeEnum getBusinessType() {
        return mBusinessType;
    }
}
